#include "llt.h"

#include <stdio.h>
#include <string.h>

#include <vfs.h>

#define FATFS_PATH(s) "/fatfs/0:" s

static void test_fatfs_mmc_demo(void **state)
{
    const char *f_name = FATFS_PATH("/f_opt.txt");
    int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;

    // open
    int fd = vfs_open(f_name, O_RDWR | O_CREAT | O_TRUNC, mode);
    assert_int_not_equal(-1, fd);

    // write
    const char *hello = "hello file\n";
    int size = vfs_write(fd, hello, strlen(hello));
    assert_int_equal(strlen(hello), size);
    printf("write : %s", hello);

    // read null from current position
    char buf[20] = {0};
    size = vfs_read(fd, buf, sizeof(buf));
    assert_int_equal(0, size);

    // set position to head
    int offset = vfs_lseek(fd, 0, SEEK_SET);
    assert_int_equal(0, offset);

    // read again
    size = vfs_read(fd, buf, sizeof(buf));
    assert_int_equal(strlen(hello), size);
    printf("read  : %s", buf);

    // close
    int ret = vfs_close(fd);
    assert_int_equal(0, ret);

    ret = vfs_unlink(f_name);
    assert_int_equal(0, ret);
}

/** test entry */

void test_fatfs_mmc(struct tester_case *caser)
{
    UnitTest tests[] = {
        unit_test(test_fatfs_mmc_demo),
    };

    caser->stats->totals = sizeof(tests) / sizeof(tests[0]);
    caser->stats->fails = run_tests(tests);
}

TESTER_CASE_DEF("test_fatfs_mmc", test_fatfs_mmc, NULL, NULL, 0);
